﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using Ext.Net;

namespace LongSpeed.WebSite.Main
{
    public partial class ChildMemu : Behaviors.Session
    {
        private int parentId
        {
            get { return Convert.ToInt32(Request.QueryString["mPartId"]); }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindChildMemu(parentId, false);
            }
        }

        [Ext.Net.DirectMethod]
        public void BindChildMemu(int parentId, bool refresh)
        {
            LongSpeed.WebSite.Behaviors.Memu pm = new Behaviors.Memu();
            pm.Init(this.parentId);
            var ms = pm.GetMemu(new Behaviors.Memu() { ID = parentId }, false);

            this.plParentMemu.Html = string.Format("<p style='padding: 10px 10px 10px 10px; color: #556677; font-size: 15px'>上级菜单：{0}</p>", pm.Name);

            this.ChildMemuStore.DataSource = ms;
            this.ChildMemuStore.DataBind();
        }
        protected void RefreshChildMemuData(object sender, StoreRefreshDataEventArgs e)
        {
            BindChildMemu(parentId, true);
        }

        [DirectMethod]
        public void ShowServerConfig(int parentId)
        {
            Window winServerConfig = null;

            winServerConfig = new Window
            {
                ID = "winServerConfig",
                Title = "服务器管理",
                Width = System.Web.UI.WebControls.Unit.Pixel(800),
                Height = System.Web.UI.WebControls.Unit.Pixel(450),
                Modal = true,
                Maximizable = true,
                Hidden = true,
                Icon = Icon.Server
            };

            winServerConfig.AutoLoad.Url = string.Format("{0}/Main/ServerConfigList.aspx?mPartId={1}", Behaviors.Session.GetRootUrl(this), parentId);
            winServerConfig.AutoLoad.Mode = LoadMode.Merge;
            winServerConfig.Render(this.vpChildMemu);

            winServerConfig.Show(this.vpChildMemu);
        }

        [DirectMethod]
        public int SaveMemu(object obj)
        {
            Ext.Net.JsonReader jo = new JsonReader();
            var memu = Newtonsoft.Json.JsonConvert.DeserializeObject(obj.ToString(), typeof(Behaviors.Memu)) as Behaviors.Memu;
            memu.ParentMemu = new Behaviors.Memu() { ID = parentId };
            if (memu.Save()) return memu.ID; else return 0;
        }
        [DirectMethod]
        public bool DeleteMemu(object obj)
        {
            Ext.Net.JsonReader jo = new JsonReader();
            var m = Newtonsoft.Json.JsonConvert.DeserializeObject(obj.ToString(), typeof(Behaviors.Memu)) as Behaviors.Memu;
            
            return m.Delete();
        }

    }
}